package nyist.media.controller;

import nyist.media.annotation.Log;
import nyist.media.enums.ApplyStatus;
import nyist.media.page.ApiResult;
import nyist.media.service.ApplyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author dcf
 * @date 2019/3/18
 */
@RestController
@RequestMapping(value = "/admin/apply_records", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public class ApplyRecordController extends BaseController {

    @Autowired
    private ApplyRecordService applyRecordService;

    /**
     * 创建申请单
     *
     * @param page   页码
     * @param limit  页长
     * @param status 申请单状态 0: 申请中 1:申请通过 2:申请失败
     * @return 申请单列表
     */
    @GetMapping
    public Map<String, Object> findPage(Integer page, Integer limit, ApplyStatus status) {
        PageRequest pageRequest = new PageRequest(page - 1, limit);
        return toResultPage(applyRecordService.findPage(pageRequest, status));
    }

    /**
     * 申请单审核
     *
     * @param id 申请单ID
     * @return 成功状态码 20000 成功信息：响应成功
     */
    @Log(value = "审核申请单")
    @PostMapping("/audit/{id}")
    public Map<String, Object> auditApplyRecord(@PathVariable Long id) {
        applyRecordService.auditApplyRecord(id, getCurrentInfo().getId());
        return ApiResult.SUCCESS.getMap();
    }

    @Log(value = "驳回申请单")
    @PostMapping("/rejected/{id}")
    public Map<String, Object> rejectedApplyRecord(@PathVariable Long id) {
        applyRecordService.rejectedApplyRecord(id);
        return ApiResult.SUCCESS.getMap();
    }

    /**
     * 查看我的申请单
     * @param page 页码
     * @param limit 页长
     * @return 我的申请单列表
     */
    @GetMapping("/my")
    public Map<String, Object> myOrders(Integer page, Integer limit) {
        PageRequest pageRequest = new PageRequest(page - 1, limit);
        return toResultPage(applyRecordService.findMyPage(pageRequest, getCurrentInfo()));
    }

    @Log(value = "删除申请单")
    @DeleteMapping("/{id}")
    public Map<String, Object> removeOrders(@PathVariable Long id) {
        applyRecordService.deleteOne(id);
        return ApiResult.SUCCESS.getMap();
    }

}
